//
// Created by 王筱童 on 2022/5/14.
//

#ifndef TINY_REDIS_SERVER_CMAKE_SLAVE_MANAGER_H
#define TINY_REDIS_SERVER_CMAKE_SLAVE_MANAGER_H
#include <string>
#include <vector>
#include <asio.hpp>
#include <memory>
#include <vector>
#include <unordered_map>

class redis_server;
class slave_manager {
    typedef std::unique_ptr<asio::ip::tcp::socket> sock_ptr;
    typedef std::pair<std::string,sock_ptr> buf_sock;
    std::unordered_map<std::size_t,buf_sock> socks;
    std::size_t sock_indexs=0;
    redis_server* server;
    asio::awaitable<void> sync(std::size_t index,const std::string&buf);
    asio::awaitable<void> sync_data(std::size_t index, const std::string buf, const std::size_t db_index);
public:
    explicit slave_manager(redis_server*_server);
    void add_slave(asio::ip::tcp::socket&& socket,const std::string& ip_port,const std::string&buf="");
    void async_sync(const std::string&command,const std::vector<std::string>&args,std::size_t db_index);
    std::vector<std::pair<std::string,std::uint16_t>> get_all_slave_ip_port()const;
};


#endif //TINY_REDIS_SERVER_CMAKE_SLAVE_MANAGER_H
